﻿using System;
using System.Linq;
using Autofac;
using JiaBao.Core.Configuration;
using JiaBao.Core.Data;
using JiaBao.Core.Infrastructure;
using JiaBao.Core.Infrastructure.DependencyManagement;
using JiaBao.Core.Plugins;
using JiaBao.Data;
using JiaBao.Services.AuctionService;
using JiaBao.Services.CustomerService;
using JiaBao.Services.ExcelService;
using JiaBao.Services.JtdjService;
using JiaBao.Services.QRCodeService;
using JiaBao.Services.SalerService;
using JiaBao.Services.ServerState;
using JiaBao.Services.StoreService;
using JiaBao.Services.WPFFormPlug;
using JiaBao.WPF.Framework.SN;

namespace JiaBao.WPF.Framework.Infrastructure
{

    public class DependecyRegistrar : IDependencyRegistrar
    {
        public int Order => 1;

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, JiaBaoConfig config)
        {
            //插件
            builder.RegisterType<PluginFinder>().As<IPluginFinder>().InstancePerLifetimeScope();

            //注册码
            var r = new RegisterManager();
            builder.Register(c => r.LoadSettings()).As<Register>();

            //数据库
            var dataSettingsManager = new DataSettingsManager();
            var dataProviderSettings = dataSettingsManager.LoadSettings();
            builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>();
            builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency();

            builder.Register(x => x.Resolve<BaseDataProviderManager>().LoadDataPrvider()).As<IDataProvider>().InstancePerDependency();

            if (dataProviderSettings != null && dataProviderSettings.IsValid())
            {
                var ef = new EfDataProviderManager(dataSettingsManager.LoadSettings());
                var dataProvider = ef.LoadDataPrvider();
                dataProvider.InitConnectionFactory();
                builder.Register<IDbContext>(c => new JiaBaoObjectContext(dataProviderSettings.DataConnectionString)).InstancePerLifetimeScope();
            }
            else
            {
                builder.Register<IDbContext>(c => new JiaBaoObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerLifetimeScope();
            }

            builder.RegisterGeneric(typeof(EfRepositor<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();


            ///本项目-------------------------------------------------------------
            //注入forms
            var types = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IRegistrarWPFForm))))
            .ToArray();
            foreach (var formtype in types)
            {
                builder.RegisterAssemblyTypes(formtype.Assembly);
            }

 

            builder.RegisterType<QRCodeService>().As<IQRCodeService>().InstancePerLifetimeScope();


            builder.RegisterType<WPFFormService>().As<IWPFFormService>().InstancePerLifetimeScope();

            builder.RegisterType<CustomerService>().As<ICustomerService>().InstancePerLifetimeScope();

            builder.RegisterType<ServerState>().As<IServerState>().InstancePerLifetimeScope();

            builder.RegisterType<AuctionService>().As<IAuctionService>().InstancePerLifetimeScope();
            builder.RegisterType<SalerService>().As<ISalerService>().InstancePerLifetimeScope();
            builder.RegisterType<JtdjService>().As<IJtdjService>().InstancePerLifetimeScope();
            builder.RegisterType<StoreService>().As<IStoreService>().InstancePerLifetimeScope();
            builder.RegisterType<ExcelService>().As<IExcelService>().InstancePerLifetimeScope();
        }
    }
}
